<?php

class Controller_Contact extends Controller_Website
{

    public function before() {
        parent::before();
        $this->section = 'contact';
        $this->template->activeCat = 'contact';
        $this->addScript('/scripts/jquery.placeholder.min.js');
    }

    public function actionIndex()
    {
        $this->template->title = $this->settings['title'];
        $this->template->content = new View('contact/index');
    }

    public function actionContact()
    {
        $this->autoRender = FALSE;
        $emailArr = $this->db->query("SELECT value FROM settings WHERE `key`='email'")->fetch();
        $shinyEmail = $emailArr['value'];

        if(Request::$isAjax)
        {
            $email = Request::get('email');
            $message = Request::get('message');
            $message = strip_tags(trim($message));
            $phone = Request::get('phone');
            $name = Request::get('name');

            $msg =  '';
            $msg .= 'You have new message from:' .$name. "\n\n";
            $msg .= 'Phone: '.$phone . "\n";
            $msg .= 'Email: '.$email . "\n";
            $msg .= 'Message:' . "\n" . $message;

            if(Validate::email($email) &&$message !== '')
            {
                Mailer::sendMail($shinyEmail, 'New message from shinyrooms', $msg, $this->settings['title'].'<noreply@'.$_SERVER['HTTP_HOST'].'>');

            }
            else
            {
                echo 'Information is not valid.Please try ag';
            }
        }

    }

    public function actionRequest(){
        $this->autoRender = false;
        if(Request::$isAjax){
            $response['errors'] = array();

            $name       = trim(Request::get('name'));
            $email      = trim(Request::get('email'));
            $phone      = htmlspecialchars(strip_tags(trim(Request::get('phone'))));
            $message    = htmlspecialchars(strip_tags(trim(Request::get('message'))));

            if(!Validate::alphacyr($name)){
                $response['errors'][] = 'Name value is not valid!';
            }
            if(!Validate::email($email)){
                $response['errors'][] = 'Email value is not valid!';
            }

            if(!count($response['errors'])){
                unset($response['errors']);

                $response['success'] = "Thanks for your request! \n We will get back to you soon";

                $msg ='';
                $msg.= 'You have new request-message from: '.$name.PHP_EOL;
                $msg.= 'Phone: '.$phone.PHP_EOL;
                $msg.= 'E-mail: '.$email.PHP_EOL;
                $msg.= 'Message: '.$message.PHP_EOL;

                Mailer::sendMail($this->settings['email'], 'New request-message from shinyrooms', $msg, $this->settings['title'].'<noreply@'.$_SERVER['HTTP_HOST'].'>');

            }
            $this->response->setHeader('Content-Type', 'application/json');
            $this->response->body = json_encode($response);
        }
    }
}
